/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

 /***************************************************************************
  * Name: client_requester.h
  *
  * Purpose: client endian base class implementation(with json interface) for 'COM' Functional
  *
  * Developer:
  *   wen.gu , 2020-06-12
  *
  * TODO:
  *
  ***************************************************************************/

  /******************************************************************************
   **    INCLUDES
   ******************************************************************************/
#ifndef __PANDA_COM_CLIENT_REQUESTER_H__
#define __PANDA_COM_CLIENT_REQUESTER_H__
#include <functional>
#include "panda/com/com_types.h"
#include "panda/core/json.h"

/******************************************************************************
 **    MACROS
 ******************************************************************************/

/******************************************************************************
 **    TYPE DEFINITIONS
 ******************************************************************************/
namespace panda
{
namespace com 
{

class ClientRequester;

/** for async get(or find)  service callback.
 *  ClientRequester* [in]  if find(or got) target service, then return the client instance of it.
 */

using ClientFindServiceHandler = std::function<void(ClientRequester*)>;


/******************************************************************************
**    CLASSES/FUNCTIONS DEFINITIONS
******************************************************************************/

class COM_CLASS ClientRequester final
{
public:
    using PandaErrc = panda::core::PandaErrc;
    using Value = panda::core::Value;
protected:
    ClientRequester();
public:
    ~ClientRequester();
public:
    /** try to find(or get) target service 
     * if failed, then this function will be return error code immediately.
     */
    static ClientRequester* getService(const std::string& srvName, ServiceWorkMode mode = ServiceWorkMode::kLocalOnly);


    /** try to find(or get) target service with async mode.
    *  this function will be return immediately,
    *  the 'handler' callback will be called
    *  only already find(or get) service successfully.
    */
    static PandaErrc getServiceAsync(const std::string& srvName, ClientFindServiceHandler handler, ServiceWorkMode mode = ServiceWorkMode::kLocalOnly);



    /**
     * a helper function for call sync method directly.
     * return : true: call method success, false: failed 
     */
    static bool methodCall(const std::string& service, const std::string& method, const Value& param, Value& reply, ServiceWorkMode mode = ServiceWorkMode::kLocalOnly);

public:
    PandaErrc request(const std::string& method, const Value& param, Value& reply, int32_t waitMs = COM_REQ_WAIT_INFINITE);

    PandaErrc requestAsync(const std::string& method, const Value& param, ClientReplyHandler rpyHandler);

    PandaErrc subscribeSignal(const std::string& sig, ClientSignalHandler sigHandler);

    PandaErrc unsubscribeSignal(const std::string& sig);
private:
    class impl;
    std::unique_ptr<impl> mImpl;
};

} /** namespace com */
} /** namespace panda */

#endif /** !__PANDA_COM_CLIENT_REQUESTER_H__ */